<?php

class ArtistsController extends Controller
{
	public function filters()
	{
		return array(
			'accessControl',
		);
	}
	/**
	 * Specifies the access control rules.
	 * This method is used by the 'accessControl' filter.
	 * @return array access control rules
	 */
	public function accessRules()
	{
		return array(
			array('allow',
				'actions'=>array('index', 'view', 'eventOld', 'eventNews'),
				'users'=>array('*'),
			),
			array('deny',
				'users'=>array('*'),
			),
		);
	}

    public function actionIndex()
    {
        $criteria = new CDbCriteria;
        $criteria->condition = 'status="A"';

        $dataProvider = new CActiveDataProvider('Artists',
            array(
                'criteria'=>$criteria,
                'sort'=>array(
                    'defaultOrder'=>'nickname ASC',
                ),
                'pagination'=>array(
                    'pageSize'=>10,
                    'pageVar'=>'page',
                ),
            )
        );
        $this->render('index', array(
            'dataProvider'=>$dataProvider
        ));
    }


	public function actionView($slug)
	{
        $this->layout = '//layouts/eventsDefaultView';
        $model = Artists::model()->findByAttributes(array('url'=>$slug));

        if (empty($model)) {
            throw new CHttpException(404,'The requested page does not exist.');
        }

        // Find Coming Events
        $criteria = new CDbCriteria;
        $criteria->condition = 'status="A" AND startdate >= NOW()';
        $criteria->limit = 5;
        $criteria->order = 'startdate ASC, enddate DESC';
        $criteria->with = array('eventArtist'=>array(
            'joinType'=>'INNER JOIN',
            'condition'=>'eventArtist.artist_id = '.$model->id,
            'together'=>TRUE
        ));

        $eventNews = Events::model()->findAll($criteria);

        // Find Past Events
        $criteria = new CDbCriteria;
        $criteria->condition = 'status="A" AND startdate < NOW()';
        $criteria->limit = 5;
        $criteria->order = 'startdate ASC, enddate DESC';
        $criteria->with = array('eventArtist'=>array(
            'joinType'=>'INNER JOIN',
            'condition'=>'eventArtist.artist_id = '.$model->id,
            'together'=>TRUE
        ));

        $eventOld = Events::model()->findAll($criteria);

        $this->render('view', array(
            'model'=>$model,
            'eventNews'=>$eventNews,
            'eventOld'=>$eventOld
        ));
	}

    public function actionEventOld($slug)
    {
        $model = Artists::model()->findByAttributes(array('url'=>$slug));

        if (empty($model)) {
            throw new CHttpException(404,'The requested page does not exist.');
        }

        $criteria = new CDbCriteria;
        $criteria->condition = 'status="A" AND startdate < NOW()';
        $criteria->with = array('eventArtist'=>array(
            'joinType'=>'INNER JOIN',
            'condition'=>'eventArtist.artist_id = '.$model->id,
            'together'=>TRUE
        ));

        $dataProvider=new CActiveDataProvider('Events',
			array(
				'criteria'=>$criteria,
				'sort'=>array(
					'defaultOrder'=>'enddate ASC, startdate DESC',
				),
				'pagination'=>array(
					'pageSize'=>10,
					'pageVar'=>'page',
				),
			)
		);

        $this->render('eventOld', array(
			'dataProvider'=>$dataProvider,
            'model'=>$model
		));
    }

    public function actionEventNews($slug)
    {
        $model = Artists::model()->findByAttributes(array('url'=>$slug));

        if (empty($model)) {
            throw new CHttpException(404,'The requested page does not exist.');
        }

        $criteria = new CDbCriteria;
        $criteria->condition = 'status="A" AND startdate >= NOW()';
        $criteria->with = array('eventArtist'=>array(
            'joinType'=>'INNER JOIN',
            'condition'=>'eventArtist.artist_id = '.$model->id,
            'together'=>TRUE
        ));

        $dataProvider=new CActiveDataProvider('Events',
			array(
				'criteria'=>$criteria,
				'sort'=>array(
					'defaultOrder'=>'enddate ASC, startdate DESC',
				),
				'pagination'=>array(
					'pageSize'=>10,
					'pageVar'=>'page',
				),
			)
		);

        $this->render('eventNews', array(
			'dataProvider'=>$dataProvider,
            'model'=>$model
		));
    }

    protected function performAjaxValidation($model)
	{
    if(isset($_POST['ajax']) && $_POST['ajax']==='order-form')
    {
        echo CActiveForm::validate($model);
        Yii::app()->end();
    }
	}
}